package com.team.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.team.entity.PageParameter;
import com.team.entity.SysUsers;
import com.team.entity.Users;
import com.team.mapper.UsersMapper;
import com.team.service.SysUsersService;
import com.team.mapper.SysUsersMapper;
import com.team.util.MD5Utils;
import com.team.util.MenuDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author asus
* @description 针对表【sys_users(用户表)】的数据库操作Service实现
* @createDate 2024-07-13 14:39:20
*/
@Service
public class SysUsersServiceImpl extends ServiceImpl<SysUsersMapper, SysUsers>
    implements SysUsersService{
    @Autowired
    private SysUsersMapper sysUsersMapper;
    @Autowired
    private UsersMapper usersMapper;
    @Override
    public SysUsers login(String username, String password) {
        QueryWrapper<SysUsers> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_name",username).eq("password", MD5Utils.md5Encrypt(password));
        SysUsers sysUsers=sysUsersMapper.selectOne(queryWrapper);
        return sysUsers;
    }

    @Override
    public PageInfo<Users> getAllUsers(PageParameter pageParameter) {
        PageHelper.startPage(pageParameter.getPage(), pageParameter.getPageSize());
        List<Users> allUsers = usersMapper.getAllUsers();
        PageInfo<Users> pageInfo=new PageInfo<>(allUsers);
        return pageInfo;
    }

    @Override
    public boolean addUser(SysUsers user) {
        user.setPassword(MD5Utils.md5Encrypt(user.getPassword()));
        int temp = sysUsersMapper.insert(user);
        if (temp>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public int updateUser(Integer id, String password, String headUrl) {
        return sysUsersMapper.updateUser(id, password, headUrl);
    }

    @Override
    public List<MenuDto> getUserMenu(Long uid) {
        //通过用户id查询对应的一级菜单
        List<MenuDto> list=sysUsersMapper.getMenuByUser(uid);
        //2.循环遍历一级菜单，查询二级菜单
        for (MenuDto menuDto : list){
            //查询二级菜单
            List<MenuDto> two=sysUsersMapper.getMenuByPid(menuDto.getId());
            //将二级菜单添加到一级菜单的子菜单中
            menuDto.setChildren(two);
        }
        return list;
    }
}




